package br.com.remedioCerto.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import br.com.remedioCerto.R;
import br.com.remedioCerto.dao.HorarioDao;
import br.com.remedioCerto.dao.PerfilDao;
import br.com.remedioCerto.dao.ReceitaDao;
import br.com.remedioCerto.dao.RemedioDao;
import br.com.remedioCerto.model.Receita;

public class Princiapal extends Activity {

	
	ListView listaReceitaView;
	ArrayAdapter<Receita> adapter;


	/************************************************************************
	*
	*                            CICLO DE VIDA
	*************************************************************************/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.principal);
		listaReceitaView  = (ListView)findViewById(R.id.receita_lista);
		registerForContextMenu(listaReceitaView);
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		ReceitaDao dao = new ReceitaDao(this);
		dao.open();
		List<Receita> listaReceita =dao.listaList();
		dao.close();
		
		RemedioDao remedioDao = new RemedioDao(this);
		remedioDao.open();
		PerfilDao perfilDao = new PerfilDao(this);
		perfilDao.open();
		for (Receita receita : listaReceita) {
			receita.setRemedio(remedioDao.findById(receita.getRemedio().getId()));
			receita.setPerfil(perfilDao.findById(receita.getPerfil().getId()));
		}
		remedioDao.close();
		perfilDao.close();
		
		adapter = new ArrayAdapter<Receita>(this, android.R.layout.simple_list_item_1,listaReceita);
		listaReceitaView.setAdapter(adapter);
	}
	

	/************************************************************************
	*
	*                                 MENU
	*************************************************************************/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater mi = getMenuInflater();
		mi.inflate(R.menu.principal_menu,menu);
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.contexto_edita_exclui, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		AdapterContextMenuInfo	info = (AdapterContextMenuInfo)item.getMenuInfo();
		
	if(item.getItemId()==R.menu_contexto.editar){
			Intent editar = new Intent(this, ReceitaForm.class);
			editar.putExtra("receitaEdicao",adapter.getItem(info.position));
			startActivity(editar);
		}
	if(item.getItemId()==R.menu_contexto.excluir){
		
		
		HorarioDao horarioDao = new HorarioDao(this);
		horarioDao.open();
		horarioDao.removerPorReceita(adapter.getItem(info.position).getId());
		horarioDao.close();
		
		ReceitaDao receitaDao = new ReceitaDao(this);
		receitaDao.open();
		receitaDao.remove(adapter.getItem(info.position).getId());
		receitaDao.close();
		
		adapter.remove(adapter.getItem(info.position));
		adapter.notifyDataSetChanged();
	}

	
	
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.menu_principal.perfil_cadastro:
	        	Intent perfilList = new Intent(this,PerfilLista.class);
	        	startActivity(perfilList);
	            return true;
	        case R.menu_principal.remedio_cadastro:
	        	Intent intent = new Intent(this,RemedioLista.class);
	        	startActivity(intent);
	            return true;
	        case R.menu_principal.preferencia:
	        	Intent pref = new Intent(this,Preferencia.class);
	        	startActivity(pref);
	            return true;
	        case R.menu_principal.receita:
	        	Intent receita = new Intent(this,ReceitaForm.class);
	        	startActivity(receita);
	            return true;
//	        case R.menu_principal.relatorio:
//	        	
//	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	

	/************************************************************************
	*
	*                            METODOS
	*************************************************************************/
	
}
